const request = require('../../utils/https.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled:false,
    loading:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options====>', options)
    let orderMoney = Number(options.orderMoney).toFixed(2);
    let orderId = options.orderId;
    let needPay = options.needPay
    this.setData({
      orderMoney: orderMoney,
      orderId: orderId
    })
  },
  //支付订单
  postSubmit(e){
    console.log('e.detail.formId===>',e.detail.formId)
    const that=this;
    that.setData({
      loading:true,
      disabled:true
    })
    request._get(getApp().Api_url + "order/payIt",{
      "userId": wx.getStorageSync("userId"),
      "orderId": this.data.orderId
    }, (res) => {

      let params = res.data.data;
      console.log('res===>', res)
      console.log('params===>', params)
      that.setData({
        loading:false
      })

      //微信支付 
      if (res.data.errMsg == 'OK') {
        // 去微信支付
        wx.requestPayment({
          'appId': params.appId,
          'timeStamp': params.timeStamp + '',
          'nonceStr': params.nonceStr,
          'package': params.packageValue,
          'signType': 'MD5',
          'paySign': params.paySign,
          'success': (payres) => {
            console.log("payres", payres)
            if (payres.errMsg == 'requestPayment:ok') {
              /**
               * 支付成功
               */
              wx.redirectTo({
                url: '/pages/pay_success/pay_success?orderMoney=' + that.data.orderMoney
              })
              that.setData({
                disabled: false
              })
 
            }
          },
          'fail': (xhr) => {
            console.log("xhr", xhr)
            if (xhr.errMsg == 'requestPayment:fail cancel') {
              wx.reLaunch({
                url: '/pages/my_order/my_order?msg=cancel'
              })


            }
          }
        })
      } 

    }, (xhr) => {

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})